<?php

namespace Beerus\Modules\BlindBox\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Biz\model\brandActivity\BlindBoxActivity as mBlindBoxActivity;
use Biz\service\brand\AdmBlindBoxLottery;
use Doraemon\model\Phone as PhoneModel;
use Doraemon\tools\HhzEnv;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use hhz\constant\basic\UserExtendPhoneScene;
use Illuminate\Http\Request;

class WinnerController extends Controller
{
    public const  PAGE_SIZE = 20;

    /**
     * 盲盒获奖列表
     * @param Request $request
     * @param Content $content
     * @return Content
     */
    public function index(Request $request, Content $content): Content
    {
        Admin::script($this->operateScript());
        $params = $request->all();
        $pageSize = $params['per_page'] ?? self::PAGE_SIZE;
        $page = $params['page'] ?? 1;
        $activityId = $params['activity_id'] ?? 0;
        $list = AdmBlindBoxLottery::getWinnerList(['page' => $page, 'page_size' => $pageSize, 'activity_id' => $activityId]);

        $activityInfo = $list['activity_info'];
        $gridModel = new GridDataModel($list['list'], $list['total'], ['perPage' => $pageSize]);
        $grid = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $activityInfo) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableActions();
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableFilter();
                $grid->disableCreateButton();
                $grid->column('uid', '用户uid');
                $grid->column('nickName', '用户昵称');
                $grid->column('prize_title', '奖品名称');
                $grid->column('prize_type', '奖品类型')->display(function () use ($activityInfo) {
                    $html = '<ul class="list-inline" style="word-break: break-all;">';
                    $html .= '<li>奖品类型：<b>' . mBlindBoxActivity::$prizeTypeDesc[$this->prize_type] . '</b></li>';
                    switch ($this->prize_type) {
                        case mBlindBoxActivity::PRIZE_TYPE_GOODS:
                            $h5Url = HhzEnv::detectShopDomain() . 'shop/goods-detail/wiki?basic_id=' . $this->basic_id . '&no-pc-redirect=true';
                            $html .= '<li>WIKI ID：<a class="fa" href="javascript:layer.open({
                                type: 2,
                                title: \'预览\',
                                maxmin: true,
                                area: [\'800px\', \'500px\'],
                                content: \'' . $h5Url . '\'
                            });">' . $this->basic_id . '</a></li>';
                            $html .= '<li>奖品名称：' . $this->prize_title . '</li>';
                            break;
                        case mBlindBoxActivity::PRIZE_TYPE_JD_CARD:
                            $html .= '<li>卡号：</li>';
                            foreach ($this->card as $item) {
                                $html .= '<li style="color: #00a65a ">「' . $item . '」</li>';
                            }
                            break;
                        case mBlindBoxActivity::PRIZE_TYPE_LIMIT_CARD:
                            $html .= '<li>卡片：</li>';
                            foreach ($this->limit_card as $img) {
                                $html .= '<li style="color: #00a65a "><img width="50" height="50" src="' . $img . '"></li>';
                            }
                            break;
                        case mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON:
                            $html .= '<li>奖品名称：' . $this->prize_title . '</li>';
                            $html .= '<li>店铺名称：' . $activityInfo['shop_name'] . '</li>';
                            $html .= '<li>优惠券码：</li>';
                            foreach ($this->coupon as $item) {
                                $html .= '<li style="color: #00a65a ">';
                                $html .= '<a href="javascript:void(0);" class="grid-column-copyable text-muted" data-content="' . $item . '" title="Copied!" data-placement="bottom"><i class="fa fa-copy"></i></a>';
                                $html .= '「' . $item . '」</li>';
                            }
                            break;
                        case mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON:
                            $h5Url = HhzEnv::detectShopDomain() . 'shop/goods-detail/wiki?basic_id=' . $this->basic_id . '&no-pc-redirect=true';
                            $html .= '<li>WIKI ID：<a class="fa" href="javascript:layer.open({
                                type: 2,
                                title: \'预览\',
                                maxmin: true,
                                area: [\'800px\', \'500px\'],
                                content: \'' . $h5Url . '\'
                            });">' . $this->basic_id . '</a></li>';
                            $html .= '<li>奖品名称：' . $this->prize_title . '</li>';
                            $html .= '<li>店铺名称：' . $activityInfo['shop_name'] . '</li>';
                            $html .= '<li>优惠券码：</li>';
                            foreach ($this->coupon as $item) {
                                $html .= '<li style="color: #00a65a ">';
                                $html .= '<a href="javascript:void(0);" class="grid-column-copyable text-muted" data-content="' . $item . '" title="Copied!" data-placement="bottom"><i class="fa fa-copy"></i></a>';
                                $html .= '「' . $item . '」</li>';
                            }
                            break;
                    }
                    $html .= '</ul>';
                    return $html;
                })->style('width:200px;');
                $grid->column('prize_price', '奖品价值');
                $grid->column('prize_img', '奖品图片')->display(function ($img_url) {
                    return '<a href="javascript:layer.open({type: 2,title: \'预览\',maxmin: true,area: [\'800px\', \'600px\'],content: \'' . $img_url . '\'});">
                                <img width="100px" height="100px" src="' . $img_url . '">
                            </a>';
                });
                $grid->column('consignee_name', '收货人');
                $grid->column('consignee_mobile', '收货人联系电话')->display(function () {
                    return $this->consignee_mobile_encrypt_id ? PhoneModel::getExtendPhone($this->consignee_mobile_encrypt_id, UserExtendPhoneScene::BLIND_BOX_LOTTERY_PHONE) : $this->consignee_mobile;
                });
                $grid->column('consign_address', '收货地址');
                $grid->column('courier_name', '物流公司');
                $grid->column('express_no', '物流单号');
            }
        );

        $tab = new Tab();
        $tab->add("中奖名单", $grid->render(), true);
        $content->row($tab);

        if ($activityInfo) {
            $header = '中奖名单' . '~' . $activityInfo['title'];
        } else {
            $header = '中奖名单';
        }

        return $content->header($header)
            ->breadcrumb(['text' => '审核列表', 'url' => '/blindBox/index']);
    }

    private function operateScript(): string
    {
        return <<<SCRIPT
        $('.grid-table').on('click', '.grid-column-copyable', (function (e) {
            var content = $(this).data('content');
            var temp = $('<input>');
            $("body").append(temp);
            temp.val(content).select();
            document.execCommand("copy");
            temp.remove();
            $(this).tooltip('show');
        }));
SCRIPT;
    }
}